package com.xiaomili.halfsugar;


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索页面---点击搜索---流布局
 * Created by Administrator on 2016/7/22.
 */
public class FlowLayout extends ViewGroup {

    private List<List<View>> mListAll;
    private List<Integer> mRowMaxHeightList;

    public FlowLayout(Context context) {
        this(context,null);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mListAll.clear(); //防止onMeasure反复调用的情况,进来全部清空只留最后一次情况
        int widthsize = MeasureSpec.getSize(widthMeasureSpec);
        int widthmode = MeasureSpec.getMode(widthMeasureSpec);
        int heightsize = MeasureSpec.getSize(heightMeasureSpec);
        int heightmode = MeasureSpec.getMode(heightMeasureSpec);
        //获取取最宽一行的宽度    获取总共的高度
        int wrapWidth = 0, wrapHeight = 0;
        //记录每一行的宽度和高度
        int lineWidth = 0, lineHeight = 0;
        //存放每一行的View
        List<View> childList = new ArrayList<View>();
        //获取自定义容器中总共的子控键个数
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);//获取每个子控件

           // 因为getMeasuredWidth在measure后就有了值，而getWidth在layout才有了值
            //这句一定要加        //第一个参数:需要测量的child ,后面2个参数宽度和高度的MeasureSpec,入参提供
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth() + lp.leftMargin ;//+ lp.rightMargin;
            int childHeight = child.getMeasuredHeight() + lp.topMargin ;//+ lp.bottomMargin;
            if (lineWidth+childWidth<widthsize) {
                lineWidth =childWidth+lineWidth;
                lineHeight=Math.max(lineHeight,childHeight);
                childList.add(child);
            }else {
                mListAll.add(childList);//添加上一行所有View
                mRowMaxHeightList.add(lineHeight);//记录上一行在最高的高度
               wrapWidth=Math.max(lineWidth,wrapWidth);
                wrapHeight+=lineHeight;
                childList = new ArrayList<View>();
                lineHeight=childHeight;
                lineWidth=childWidth;
                childList.add(child);
            }
            //当添加到最后一个元素的时候,补上最后当前行的自适应宽度和高度
            if (i == childCount - 1) {
                //最后一个child的情况 (记录的是当前行)
                //当当前这样的childList放到全局的集合中
                mListAll.add(childList);
                mRowMaxHeightList.add(lineHeight);   //当前行的最大高度
                wrapWidth = Math.max(lineWidth, wrapWidth);
                wrapHeight += lineHeight;
            }
        }
        //   EXACTLY：表示设置了精确的值，一般当childView设置其宽、高为精确值、match_parent时，ViewGroup会将其设置为EXACTLY；
        //AT_MOST：表示子布局被限制在一个最大值内，一般当childView设置其宽、高为wrap_content时，ViewGroup会将其设置为AT_MOST；
       // UNSPECIFIED：表示子布局想要多大就多大，一般出现在AadapterView的item的heightMode中、ScrollView的childView的heightMode中；此种模式比较少见。
        //如果widthmode == MeasureSpec.AT_MOST说明布局是自适应模式，就用我们自己测的
        int lastWidth = widthmode == MeasureSpec.AT_MOST ? wrapWidth : widthsize;
        int lastHeight = heightmode == MeasureSpec.AT_MOST ? wrapHeight : heightsize;
        //通过系统提供的方法进行重新测量
        setMeasuredDimension(lastWidth, lastHeight);
    }


    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }



    /**
     * 放置位置的方法
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int left = 0, right = 0, top = 0, bottom = 0; //每个child的坐标点都要动态赋值的
        int previousHeight = 0;
        for (int i = 0; i < mListAll.size(); i++) {
            List<View> childList = mListAll.get(i);
            for (int j = 0; j < childList.size(); j++) {
                View child = childList.get(j);
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                left += lp.leftMargin;
                top = previousHeight + lp.topMargin;
                right=left+child.getMeasuredWidth();
                bottom=top+child.getMeasuredHeight();
                child.layout(left,top,right,bottom);//设置子控件放的位置
                left=left+child.getMeasuredWidth();
            }
            previousHeight=previousHeight+mRowMaxHeightList.get(i);
            left=0;
        }
    }
    //初始化---定义全局的数据源
    private void init() {
        mListAll = new ArrayList<List<View>>();
        mRowMaxHeightList = new ArrayList<Integer>(); //当前行的最大高度
    }
}
